<template>
  <div class="login">
    <v-header :title="this.$route.meta.title"></v-header>
    <div class="login-header">
      <img
        src="https://accountstatic.vivo.com.cn/accountstatic.vivo.com.cn/static/img/logo.3c33210.png.webp"
        alt=""
      />
      <span>账号登录</span>
    </div>
    <div class="login-input">
      <label for>
        <span>账号</span>
        <input
          type="text"
          v-model="user.name"
          placeholder="请输入账号/用户名"
        />
      </label>

      <label for>
        <span>密码</span>
        <input
          type="password"
          v-model="user.password"
          placeholder="请输入密码"
          ref="psdRef"
        />
        <img
          src="../../../../static/img/by.png"
          alt=""
          class="psd"
          @click="changePsd"
          ref="ImgRef"
        />
      </label>
    </div>

    <div class="login-btn">
      <input type="button" class="btn" @click="login" value="登陆" />
    </div>
    <div class="login-btn">
      <input type="button" class="btn" @click="jumpRegister" value="注册" />
    </div>
  </div>
</template>
<script>
import header from "@/components/header/index.vue";
export default {
  components: {
    "v-header": header,
  },
  data() {
    return {
      user: {
        name: "",
        password: "",
      },
    };
  },
  methods: {
    jumpRegister() {
      this.$router.push("/register");
    },
    login() {
      const account = JSON.parse(localStorage.getItem("user") || "[]");
      if (account == "[]") {
        this.$toast({
          position: "middle",
          message: "该用户不存在",
        });
        return;
      }
      if (this.user.name == "") {
        this.$toast({
          position: "middle",
          message: "请输入用户名",
        });
        return;
      }

      if (this.user.password == "") {
        this.$toast({
          position: "middle",
          message: "请输入密码",
        });
        return;
      }
      const userFlag = account.some((item) => {
        return (
          this.user.name == item.name && this.user.password == item.password
        );
      });
      // console.log(userFlag);
      if (userFlag) {
        this.$toast({
          position: "middle",
          message: "登录成功",
        });
        sessionStorage.setItem(
          "token",
          "ASHIDASAHFASHFSAHHSKFH23123JKAHFJKAZHAU3QH5H3K"
        );
        sessionStorage.setItem("user", JSON.stringify(this.user));
        setTimeout(() => {
          this.$router.push("/home");
        }, 1000);
      } else {
        this.$toast({
          position: "middle",
          message: "用户名或密码错误",
        });
      }
    },
    changePsd() {
      if (!this.pwdFlag) {
        this.$refs.psdRef.type = "text";
        this.$refs.ImgRef.src = require("../../../../static/img/zy.png");
        this.pwdFlag = true;
        return;
      }
      if (this.pwdFlag) {
        this.$refs.psdRef.type = "password";
        this.$refs.ImgRef.src = require("../../../../static/img/by.png");
        this.pwdFlag = false;
        return;
      }
    },
  },
};
</script>
<style lang="scss">
.login {
  background: #fff;
  .login-header {
    margin-top: 100px;
    width: 100%;
    height: 100%;
    img {
      width: 112px;
      height: 30px;
      display: block;
      margin: 40px auto 30px;
    }
    span {
      color: black;
      display: block;
      text-align: center;
      font-size: 18px;
      line-height: 18px;
    }
  }
  .login-input {
    margin: 22px 0;
    label {
      width: 90%;
      margin: auto;
      height: 56px;
      border-bottom: 1px solid #f2f2f2;
      display: flex;
      position: relative;
      .psd {
        display: block;
        width: 26px;
        height: 26px;
        position: absolute;
        right: 20px;
        top: 15px;
      }
      span {
        width: 20%;
        font-weight: bold;
        font-size: 16px;
        line-height: 56px;
        display: block;
        float: left;
      }
      input {
        width: 80%;
        font-size: 16px;
        border: 0;
        padding-left: 12px;
      }
    }
  }
  .login-btn {
    display: flex;
    .btn {
      width: 338px;
      height: 42px;
      margin: 0 auto 12px;
      text-align: center;
      line-height: 42px;
      font-size: 16px;
      color: #fff;
      border-radius: 21px;
      border: 0;
      background-color: #46a1fa;
      background-image: linear-gradient(90deg, #418eff, #4566ff);
    }
  }
}
</style>
